package cn.dankal.flyaudio.pop;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.alibaba.fastjson.JSON;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.pojo.ProductAttrEntity;
import cn.dankal.basiclib.pojo.ProductDetailAttrEntity;
import cn.dankal.basiclib.pojo.ProductDetailItemEntity;
import cn.dankal.basiclib.pojo.ProductEntity;
import cn.dankal.basiclib.pojo.ShopCarEntity;
import cn.dankal.basiclib.pojo.req.ShoppingCarItemReq;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.ProductAttrAdapter;
import cn.dankal.flyaudio.entity.ShoppingOrderEntity;
import cn.dankal.flyaudio.friend.AddFriendActivity;
import cn.dankal.flyaudio.friend.CreateGroupByPasswordActivity;
import cn.dankal.flyaudio.store.ConfirmOrderActivity;
import cn.dankal.flyaudio.store.ShoppingCarActivity;
import razerdp.basepopup.BasePopupWindow;

public class AddShopCarPop extends BasePopupWindow implements View.OnClickListener {

    private ProductEntity entity;

    private ImageView productIconView;

    private TextView productNameView;

    private TextView productPriceView;

    private TextView numView;

    private int num = 1;

    private boolean isBuy = false;

    private RecyclerView attrList;

    private AddShopCarListener listener;

    private ProductAttrAdapter adapter;

    private String skuItemDesc = "";

    public AddShopCarPop(Context context) {
        super(context);
    }

    public AddShopCarPop setAddShopListener(AddShopCarListener listener) {
        this.listener = listener;
        return this;
    }

    public AddShopCarPop setShop(ProductEntity entity) {
        this.entity = entity;
        setData();
        return this;
    }

    public AddShopCarPop isBuy() {
        this.isBuy = true;
        TextView buy = findViewById(R.id.tv_add_car);
        buy.setText("立即购买");
        return this;
    }
    private ProductDetailItemEntity curSku;

    public void setCurrentSku(ProductDetailItemEntity sku) {
        this.curSku = sku;
        productPriceView.setText("¥" + curSku.getDiscountedPrice());
        Picasso.with(getContext()).load(curSku.getImgUrl()).into(productIconView);

    }
    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.pop_add_shop_car);
    }

    private void setData() {
        if (entity == null) return;
        Picasso.with(getContext()).load(entity.getMainImg()).into(productIconView);
        productNameView.setText("已选:" + entity.getProductName());
        productPriceView.setText("¥" + entity.getShowPrice());
        attrList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ProductAttrAdapter(entity.getItems(),this);
        attrList.setAdapter(adapter);
        List<ProductAttrEntity> attrs = entity.getAllAttrOp();
        adapter.addDatas(attrs, true);
    }

    @Override
    public void onViewCreated(View contentView) {
        super.onViewCreated(contentView);
        productIconView = findViewById(R.id.iv_product_img);
        productNameView = findViewById(R.id.tv_product_name);
        productPriceView = findViewById(R.id.tv_product_price);
        attrList = findViewById(R.id.recyclerview);
        numView = findViewById(R.id.tv_num);

        getContentView().findViewById(R.id.iv_del).setOnClickListener(this);
        getContentView().findViewById(R.id.iv_add).setOnClickListener(this);
        getContentView().findViewById(R.id.iv_close).setOnClickListener(this);
        getContentView().findViewById(R.id.tv_add_car).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.iv_close == id) {
            dismiss(true);
        } else if (R.id.iv_del == id) {
            if (num == 1) {
                ToastUtils.showShort("无法再减少啦!");
                return;
            }
            num--;
            numView.setText(num + "");
        } else if (R.id.iv_add == id) {
            num++;
            numView.setText(num + "");
        } else if (R.id.tv_add_car == id) {
            if (listener != null) {
//                ToastUtils.showShort(JSON.toJSONString(adapter.getParams()));
                List<ProductAttrEntity> attrs = entity.getAllAttrOp();
                if(attrs!=null && !attrs.isEmpty()&& attrs.size()>1){
                    if(adapter.getParams().isEmpty() || adapter.getParams().size() != attrs.size()){
                        ToastUtils.showShort("请选择sku!");
                        return;
                    }
                }
                if (entity.getItems() == null || entity.getItems().isEmpty()) {
                    listener.doAdd(num, "-1", "");
                }else if(entity.getItems().size() == 1){
                    if (isBuy) {
                        listener.doBuy(num,entity.getItems().get(0).getUuid(), "");
                        return;
                    }
                    listener.doAdd(num,entity.getItems().get(0).getUuid(), "");
                }else {
                    if (adapter.getParams().isEmpty()) {
                        ToastUtils.showShort("请选择规格!");
                        return;
                    }
                    //组合sku描述
                    StringBuilder sb = new StringBuilder();
                    for (String key : adapter.getParams().keySet()) {
                        String skuValue = adapter.getParams().get(key);
                        sb.append(skuValue);
                        sb.append(" ");
                    }
                    sb.append(num + "件");
                    if (isBuy) {
                        listener.doBuy(num, handlerParams(), sb.toString());
                        return;
                    }
                    listener.doAdd(num, handlerParams(), sb.toString());
                }
            }
            dismiss();
        }
    }

    private String handlerParams() {
        Map<String, String> params = adapter.getParams();
        int count = 0;
        List<ProductDetailItemEntity> items = entity.getItems();
        for (ProductDetailItemEntity productDetailItemEntity : items) {
            List<ProductDetailAttrEntity> attrEntities = productDetailItemEntity.getAttrList();
            if (attrEntities == null || attrEntities.isEmpty()) {
                return productDetailItemEntity.getUuid();
            }
            for (ProductDetailAttrEntity attr : attrEntities) {
                if (TextUtils.equals(params.get(attr.getAttrKey()), attr.getAttrValue())) {
                    //参数匹配
                    count++;
                }
            }
            if (count == params.size()) {
                //确认此参数
                return productDetailItemEntity.getUuid();
            } else {
                count = 0;
            }
        }
        return "";
    }

    public interface AddShopCarListener {
        void doAdd(int num, String itemUuid, String itemDesc);

        void doBuy(int num, String itemUuid, String itemDesc);
    }
}
